clear all
% close all
fclose all

CellsName='s4_tdTMLI'; %input variable as string so I can use name to save to mat file easily.
lobuleCellIdx=[ 6 8 15 17 23 24 28]     ;%Cell idx lobule boundary, i=1 ==last cell of lobule I, i=2 == last cell second lobule etc.

path1='C:\Users\Tom�s\Documents\1. Regehr Lab\Data\HCR\OTRcrex Ai14 Slc6a5 Aldh1a3 12.7.20\Processing\4. position measurements\';
path2=CellsName(1:2);

Cells = csvread([path1 path2 '\' CellsName '.csv'] ,1,1); %read cell locations
Cells=Cells(:,[1:2]); %remove last column, it contains the channel from which data was taken, irrelevant here.


lobulePCboundaries=[48 72 138 166 179 190 196]  ; 
% lobulePCboundaries=lobulePCboundaries;

if or(~(numel(lobuleCellIdx) ==7), any(diff(lobuleCellIdx)<0))
    
  error('enter correct lobuleCellIdx')
end

%load PC and ML outlines
PC=csvread([path1 path2 '\' path2 '_PCoutline.csv'] ,1,1); %read cell locations
PC=PC(:,[1:2]); %remove last column, it contains the channel from which data was taken, irrelevant here.

ML=csvread([path1 path2 '\' path2 '_MLoutline.csv'] ,1,1); %read cell locations
ML=ML(:,[1:2]); %remove last column, it contains the channel from which data was taken, irrelevant here.




%remove first row (NaNs)
if isnan(PC(1))
PC=PC(2:end,:); 
ML=ML(2:end,:);
else 
end

if isnan(Cells(1))
Cells=Cells(2:end,:); 
else 
end

%close polygons by adding last point=first point, do this only if it has
%not been done before
if sum(PC(1,:)==PC(end,:))<2
PC=vertcat(PC,PC(1,:));
ML=vertcat(ML,ML(1,:));
else
end


%determine if cell is below PC layer using inpolygon function
belowPC=inpolygon(Cells(:,1),Cells(:,2),PC(:,1),PC(:,2));

%plot shit
figure;
plot(PC(:,1),PC(:,2));
hold on
plot(ML(:,1),ML(:,2));

temp=[1 lobuleCellIdx size(Cells,1)];
for l=2:numel(temp)
    
    scatter(Cells([temp(l-1):temp(l)],1),Cells([temp(l-1):temp(l)],2));
end
    clear l temp i

% 
% scatter(Cells(belowPC,1),Cells(belowPC,2),'filled','k');
% scatter(Cells(~belowPC,1),Cells(~belowPC,2),'filled','r');

[xyPCL, distancePCL, t]=distance2curve(PC,Cells,'linear');
distancePCL(belowPC)=distancePCL(belowPC)*-1; % self explanatory

[xyML, distanceML, t]=distance2curve(ML,Cells,'linear');

%same but with different function.
% dmin = p_poly_dist1(Cells(:,1)',Cells(:,2)',PC(:,1)',PC(:,2)')
% dmin(belowPC) = dmin(belowPC)*-1


MLlength=distanceML+distancePCL;  %if above ML then it's the sum, if below then distancePCL should be substracted but it's already negative after modification, so a sum still works.



%% 
% 
save([path1 path2 '\' CellsName '.mat' ])

% save([path1 's1b2CCs.mat'],'lobulePCboundaries','-append')






%% section to plot indices of cells in case I forgot to label the lobule IDX 

x=PC(:,1);
y=PC(:,2);
figure;

% Turn hold on so all the points can 
% be plotted individually
hold on;

% plot(PC(:,1),PC(:,2))
% Calculation for the amount by which the
% label should be displaced in the 'y'
% direction
lbl_dwn = .1*max(y);
% plot and label the individual points
for i = 1:length(x)
     plot(x(i),y(i),'r.');
     % Label the points with the index
     text(x(i),y(i),num2str(i));
end
